<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/18
 * Time: 22:09
 */

namespace App\Library\form_builder\src\view\Engine;


class Blade
{
    protected $tag=['{{','}}'];

    //关键字标记
    public $key_tag='#';
    protected $rule=[
        'value'=>['foreach','endforeach'],
        'if'=>['if','endif'],
        'js'=>['js','endjs'],
        'css'=>['css','endcss'],
        'include'=>['include','endinclude']
    ];
    //组件名
    public $component_name='';

    //模板内容
    public $template='';

    //渲染后的组件
    public $components=[];

    public $component='';

    public $html='';
    /**
     * @param $find 搜索
     * @param $replace 替换
     */
    private function _replace($find, $replace,$template)
    {
        $pattern = "/{$this->tag[0]}\s*{$find}\s*{$this->tag[1]}/is";
        $content = preg_replace(
            $pattern,
            $replace,
            $template
        );
        return $content;
    }

    /**
     * 关键字搜索替换
     * @param $tag
     * @param $find
     * @param $replace
     */
//    private function custom_replace($tag,$find,$replace){
//        $left=$this->key_tag.$tag[0];
//        $right=$this->key_tag.$tag[1];
//        $pattern = "/{$left}\s*{$find}\s*{$right}/is";
//        $this->template = preg_replace(
//            $pattern,
//            $replace,
//            $this->template
//        );
//
//    }
    /**
     * 执行替换
     */
    public function execute(){

        //找到占位符就替换
        foreach($this->data as $key=>$value){
            if(is_string($value)) {
                $this->template=$this->_replace($key, $value,$this->template);
            }
        }

    }
    //清除多余的占位符 {{.*?}}
    public function clear_placeholder(){
        $pattern = "/{$this->tag[0]}\s*(.*?)\s*{$this->tag[1]}/is";
        $this->html = preg_replace(
            $pattern,
            '',
            $this->html
        );
    }
    //加载表单按钮组件
    public function load_submit($html){
        $this->component.=$html;
    }
    //加载布局文件
    public  function load_layout($html){
        $pattern = "/{$this->tag[0]}\s*(.*?)\s*{$this->tag[1]}/is";
        $this->html=preg_replace(
            $pattern,
            $this->html,
            $html
        );
        return $this->html;
    }

    public  function load_form($html,$action,$param=[]){
        $content=$this->_replace('content',$this->component,$html);
        $content=$this->_replace('action',$action,$content);
        if(!empty($param)){
            $props=$this->set_props($param);
            $content=$this->_replace('param',$props,$content);
        }
        $this->html=$content;

    }
    //设置属性
    public function set_props($props){
        $join='';
        if(is_array($props)){
            foreach($props as $item=>$value){
                if(is_numeric($item)){
                    throw new \Exception("键不能为数值");
                }else {
                    $join.="{$item}='{$value}".' ';
                }
            }
        }else if(is_string($props)){
            if($this->check_prop($props)){
                $join=$props;
            }
        }
        return $join;
    }
    //表单重组
    public function form_regroup($keys=[]){
        $this->component='';
        foreach ($keys as $key){
            if(in_array($key,$this->components)){
                $this->component.=$this->components[$key];
            }else{
                throw new \Exception('有组件未添加');
            }
        }
//        $content='';
//        foreach ($this->components as $key=>$value){
//            $content.=$value.PHP_EOL;
//        }
//        $this->component=$content;
        //return $content;
    }
    //写入
    public  function write(){
        $this->components[$this->component_name]=$this->template;
        $this->component.=$this->template;
    }
    //遍历多项
    public function loop_option($data){
        $html='';
        foreach($data as $item=>$value){
            $html.="<option value={$item}>$value</option>";
        }
        return $html;
    }
}